package ModelLayer;
import java.util.ArrayList;

/**
 * Write a description of class LoanLibrary here.
 * 
 * @author (DM80 Group 5, Anders, Michael, Jesper, Lars og Kenni) 
 * @version (16/11-2012)
 */
public class LoanLibrary
{
    private ArrayList<Loan> loans;

    private static LoanLibrary instance;
    
    private LoanLibrary()
    {
        loans = new ArrayList<Loan>();
    }
    
    public static LoanLibrary getLoanLibrary()
    {
        if(instance == null)
        {
            instance = new LoanLibrary();
        }
        
        return instance;
    }
    
    public ArrayList<Loan> getAllLoans()
    {
        return loans;
    }
    
    public void addLoan(Loan _loan)
    {
        loans.add(_loan);
    }
    
    public void deleteLoan(Loan _loan)
    {
        loans.remove(_loan);
    }
    
    public Loan searchLoanById(int _id)
    {
        boolean found = false;
        int index = 0;
        Loan retLoan = null;
        
        while(!found && index < loans.size())
        {
            Loan loan = loans.get(index);
            
            if(loan.getId() == _id)
            {
                found = true;
                retLoan = loan;
            }
            else
            {
                index++;
            }
        }
        
        return retLoan;
    }
}
